package com.erp.fin.controller;

import java.util.*;

import com.erp.fin.domain.BillingDO;
import com.erp.fin.service.BillingService;
import com.erp.itm.domain.ProjectDO;
import com.erp.itm.domain.ProjectFileDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tika.Tika;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.fin.domain.BillingFileDO;
import com.erp.fin.service.BillingFileService;
import com.erp.common.utils.PageUtils;
import com.erp.common.utils.Query;
import com.erp.common.utils.R;

/**
 * 
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2020-01-08 16:06:20
 */
 
@Controller
@RequestMapping("/fin/billingFile")
public class BillingFileController {
	@Autowired
	private BillingFileService billingFileService;
	@Autowired
	private BillingService billingService;
	
	@GetMapping()
	String BillingFile(){
	    return "fin/billingFile/billingFile";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BillingFileDO> billingFileList = billingFileService.list(query);
		int total = billingFileService.count(query);
		PageUtils pageUtils = new PageUtils(billingFileList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "fin/billingFile/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		BillingFileDO billingFile = billingFileService.get(id);
		model.addAttribute("billingFile", billingFile);
	    return "fin/billingFile/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( BillingFileDO billingFile){
	/*	if(billingFileService.save(billingFile)>0){
			return R.ok();
		}*/
		if(billingFile.getBillingId() != null){
			String [] fileNames = billingFile.getFileName().split(",");
			for (String filename:fileNames) {
				billingFile.setFileName(filename);
				billingFile.setCretaTime(new Date());
				billingFileService.save(billingFile);
			}
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( BillingFileDO billingFile){
		billingFileService.update(billingFile);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(billingFileService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		billingFileService.batchRemove(ids);
		return R.ok();
	}
	@GetMapping("/getFiles/{id}")
	public String getFiles(@PathVariable("id") Long id,Model model){
//		model.addAttribute("id", id);
		BillingDO billingDO = billingService.get(id);
		model.addAttribute("billingDO", billingDO);
		return "fin/billing/billfiles";
	}


	@ResponseBody
	@GetMapping("/getFiles/api/{id}")
	public R getFilesApi(@PathVariable("id") Long id,Model model){
		//查询全部文件
		Map<String, Object> map = new HashMap<>();
		map.put("billingId",id);
		List<BillingFileDO> billingFileDOList = billingFileService.list(map);
		List<Map> dataList = new ArrayList<>();
		if(billingFileDOList != null){
			for (BillingFileDO billingFileDO:billingFileDOList) {
				Map<String, Object> m = new HashMap<>();
				m.put("updateTime", billingFileDO.getCretaTime());  // 修改时间
				m.put("isDir", 0);  // 修改时间
				m.put("url",billingFileDO.getFileName());  // 文件地址
				String filename = billingFileDO.getFileName().replace("/files/","");
				m.put("name",filename);  // 文件地址
				String contentType = null;
				String suffix = billingFileDO.getFileName().substring(billingFileDO.getFileName().lastIndexOf(".") + 1);
				contentType = new Tika().detect(billingFileDO.getFileName());
				String type=null;
				// 获取文件图标
				if ("ppt".equalsIgnoreCase(suffix) || "pptx".equalsIgnoreCase(suffix)) {
					type = "ppt";
				} else if ("doc".equalsIgnoreCase(suffix) || "docx".equalsIgnoreCase(suffix)) {
					type = "doc";
				} else if ("xls".equalsIgnoreCase(suffix) || "xlsx".equalsIgnoreCase(suffix)) {
					type = "xls";
				} else if ("pdf".equalsIgnoreCase(suffix)) {
					type = "pdf";
				} else if ("html".equalsIgnoreCase(suffix) || "htm".equalsIgnoreCase(suffix)) {
					type = "htm";
				} else if ("txt".equalsIgnoreCase(suffix)) {
					type = "txt";
				} else if ("swf".equalsIgnoreCase(suffix)) {
					type = "flash";
				} else if ("zip".equalsIgnoreCase(suffix) || "rar".equalsIgnoreCase(suffix) || "7z".equalsIgnoreCase(suffix)) {
					type = "zip";
				} else if ("zip".equalsIgnoreCase(suffix) || "rar".equalsIgnoreCase(suffix) || "7z".equalsIgnoreCase(suffix)) {
					type = "zip";
				} else if (contentType != null && contentType.startsWith("audio/")) {
					type = "mp3";
				} else if (contentType != null && contentType.startsWith("video/")) {
					type = "mp4";
				}else {
					type = "file";
				}
				m.put("type", type);
				dataList.add(m);
			}
		}
		return R.ok().put("data",dataList);
	}

}
